package com.sunshine.androidjavapractise.pattern.observer;

import java.util.ArrayList;
import java.util.List;

public class Object3D implements Subject {
    private List<Observer> mObservers = new ArrayList<>();
    private String mMsg;

    @Override
    public void registerObserver(Observer observer) {
        mObservers.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        int index = mObservers.indexOf(observer);
        if (-1 != index) {
            mObservers.remove(observer);
        }
    }

    @Override
    public void notifyObserver() {
        for (Observer observer : mObservers) {
            observer.update(mMsg);
        }
    }

    public void setMsg(String msg) {
        mMsg = msg;
        notifyObserver();
    }
}
